import { NextRequest, NextResponse } from 'next/server';
import prisma from '@/lib/prisma';
import { ResponseUtil } from '@/utils/response';

export async function GET(request: NextRequest) {
  try {
    // 先查询所有论坛分类
    const categories = await prisma.forumCategory.findMany({
      include: {
        sections: {
          where: {
            parentId: null, // 只查询一级板块
          },
          include: {
            moderator: {
              select: {
                id: true,
                nickname: true,
                avatar: true,
              },
            },
            parent: {
              select: {
                id: true,
                name: true,
              },
            },
            children: {
              select: {
                id: true,
                name: true,
              },
            },
            _count: {
              select: {
                posts: true,
                favorites: true,
              },
            },
          },
          orderBy: [
            { sort: 'desc' },
            { postCount: 'desc' },
            { createdAt: 'desc' },
          ],
        },
      },
      orderBy: {
        id: 'asc',
      },
    });

    // 构建树形结构数据
    const treeData = await Promise.all(categories.map(async category => {
      const sectionsWithTodayCount = await Promise.all(category.sections.map(async section => {
        // 计算今日帖子数
        const today = new Date();
        today.setHours(0, 0, 0, 0);
        
        const todayPostCount = await prisma.forumPost.count({
          where: {
            sectionId: section.id,
            createdAt: {
              gte: today,
            },
            status: { not: 'REJECTED' },
            isDeleted: false,
          },
        });

        return {
          id: section.id,
          name: section.name,
          description: section.description,
          coverUrl: section.coverUrl,
          categoryId: section.categoryId,
          category: {
            id: category.id,
            name: category.name,
          },
          moderatorId: section.moderatorId,
          moderator: section.moderator,
          createdAt: section.createdAt.toISOString(),
          lastPostAt: section.lastPostAt.toISOString(),
          postCount: section.postCount,
          todayPostCount, // 添加今日帖子数
          sort: section.sort,
          parentId: section.parentId,
          parent: section.parent,
          children: section.children,
          announcement: section.announcement,
          favoriteCount: section.favoriteCount,
          updatedAt: section.updatedAt.toISOString(),
        };
      }));

      return {
        id: category.id,
        name: category.name,
        createdAt: category.createdAt.toISOString(),
        updatedAt: category.updatedAt.toISOString(),
        sections: sectionsWithTodayCount,
      };
    }));

    return ResponseUtil.success(treeData);
  } catch (error) {
    console.error('获取论坛板块失败:', error);
    return ResponseUtil.error('获取论坛板块失败', 500);
  }
} 